//
//  LanguageMO.m
//  Groups
//
//  Created by Mathieu on 24/07/06.
//  Copyright 2006 __MyCompanyName__. All rights reserved.
//

#import "LanguageMO.h"
#import "SmartGroup.h"
#import "ManualGroupMO.h"
#import "WordMO.h"

@implementation LanguageMO

- (void)awakeFromInsert{
	//create the first Manual Group to add the new word to.
	ManualGroupMO *newManualGroup = [NSEntityDescription insertNewObjectForEntityForName:@"ManualGroup" inManagedObjectContext:[self managedObjectContext]];
	[newManualGroup setValue:self forKey:@"language"];
	[newManualGroup setValue:@"First Words" forKey:@"name"];
	
	//create a new word
	WordMO *firstWord = [NSEntityDescription insertNewObjectForEntityForName:@"Word" inManagedObjectContext:[self managedObjectContext]];
	[firstWord setValue:@"Welcome" forKey:@"word"];
	[firstWord setValue:self forKey:@"language"];
	
	//add the new word to the new manual groups too
	NSMutableSet *newManualGroupsWordSet = [newManualGroup mutableSetValueForKey:@"words"];
	[newManualGroupsWordSet addObject:firstWord]; 
}

- (void)setName:(NSString *)newName{
	[self willChangeValueForKey:@"name"];
    [self setPrimitiveValue:newName forKey:@"name"];
	NSNotification *updatePredicates = [NSNotification notificationWithName:@"LanguageNameDidChange" object:newName];
	[[NSNotificationCenter defaultCenter] postNotification:updatePredicates];
    [self didChangeValueForKey:@"name"];
}

- (void)languageNameDidChange{	
	//reset this language's library smart group's predicate.
	NSLog(@"Updating language Name via notification.");
	NSMutableSet *setOfGroups = [self mutableSetValueForKey:@"groups"];
	[setOfGroups makeObjectsPerformSelector:@selector(updateLibrarysLanguageName)];
}

- (NSPredicate *)capabilityKeyPredicate{
	NSPredicate *capabilityKeyPredicateForThisLanguage = [NSPredicate predicateWithFormat:@"capabilityKey like[c] %@", [self valueForKey:@"capability"]];
	return capabilityKeyPredicateForThisLanguage;
}

#pragma mark Dictionaries

//returns a set of fetched dictionary objects that match the currently selected language.
//This does NOT return dictionaries that are actually IN USE by this language, however.
-(NSSet *)setOfDictionariesForCurrentLanguage{
	
	NSManagedObjectContext *moc = [self managedObjectContext]; 
	NSEntityDescription *entityDescription = [NSEntityDescription entityForName:@"Dictionary" inManagedObjectContext:moc]; 
	
	NSFetchRequest *request = [[[NSFetchRequest alloc] init] autorelease]; 
	[request setEntity:entityDescription]; 
	
	//Set example predicate and sort orderings...
	//the predicate is based on the language name of this language matching the pivot language of the dictionaries in the moc
	NSPredicate *languagePredicate = [NSPredicate predicateWithFormat:@"pivotLanguage LIKE[c] %@", [self valueForKey:@"capability"]];
	[request setPredicate:languagePredicate]; 
		
	NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES]; 
	[request setSortDescriptors:[NSArray arrayWithObject:sortDescriptor]]; 
	[sortDescriptor release]; 
	
	NSError *error = nil; 
	NSArray *availableDicts = [moc executeFetchRequest:request error:&error]; 
	if (availableDicts == nil) 
	{ 
		NSLog(@"Fetch request had an error.");
		// Deal with error... 
		// use an empty set in the case where something went awry
	} 
	
    return [NSSet setWithArray:availableDicts];
	
}
-(NSImage *)languageIcon{
	NSData *imageData; //not doing caching
	imageData = [self valueForKey:@"languageIconData"];
	if( imageData != nil){
		imageCache = [[NSKeyedUnarchiver unarchiveObjectWithData:imageData] retain];
	}
	return imageCache;
	
}

-(void)setLanguageIcon:(NSImage *)newImage{
	if (imageCache != newImage){
		[imageCache release];
		
		if(newImage == nil){
			NSLog(@"newImagesArray was nil");
		}
		
		//cache and archive in managedObject.
		imageCache = [newImage retain];
		NSData *imagesBlobData = [NSKeyedArchiver archivedDataWithRootObject:newImage];
		[self setValue:imagesBlobData forKey:@"languageIconData"];
	}
}


#pragma mark Archiving Support
- (void)encodeWithCoder:(NSCoder *)coder
{
    //[super encodeWithCoder:coder];
	[coder encodeObject:[self valueForKey:@"capability"] forKey:@"LCapability"];
	[coder encodeObject:[self valueForKey:@"languageIconData"] forKey:@"LLanguageIconData"];
	[coder encodeObject:[self valueForKey:@"name"] forKey:@"LName"];
	[coder encodeObject:[self valueForKey:@"nativeName" forKey:@"LNativeName"]];

    return;
}


- (id)initWithCoder:(NSCoder *)coder
{
	LanguageMO *lang = [NSEntityDescription insertNewObjectForEntityForName:@"Language" inManagedObjectContext:[[NSApp delegate] managedObjectContext]];
	[lang setValue:[coder decodeObjectForKey:@"LCapability"] forKey:@"capability"];
	[lang setValue:[coder decodeObjectForKey:@"LLanguageIconData"] forKey:@"languageIconData"];
	[lang setValue:[coder decodeObjectForKey:@"LName"] forKey:@"name"];  
	[lang setValue:[coder decodeObjectForKey:@"LNativeName"] forKey:@"nativeName"];
	return self;
}

- (NSString *)addWordToLanguageButtonTitle{
	return [NSString stringWithFormat:@"Add to %@", [self valueForKey:@"name"]];
}

-(NSString *)localisedName{
	//get this user's localised language selection
	
}

-(NSString *)localNameInLanguage:(LanguageMO *)language{
	
}



@end
